package com.gao.component;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @author gaopeng
 * @time 2020/10/26 15:39:38
 */
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        boolean bb = metaObject.hasSetter("createTime");
        Object value = getFieldValByName("createTime", metaObject);
        if (bb && value == null) {
            System.out.println("insertFill: " + metaObject.getOriginalObject());
            strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now());
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        boolean bb = metaObject.hasSetter("updateTime");
        Object value = getFieldValByName("updateTime", metaObject);
        if (bb && value == null) {
            System.out.println("updateFill: " + metaObject.getOriginalObject());
            strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
        }
    }
}
